$(document).ready(function(){
	$("#modifyUser").val(username);
	
	var src = tl_config.his_url;
	var i = src.indexOf('?');
	
	if(i!=-1){
		var cateId = src.substring(i+8);
        $.ajax({
        	url:server_url+'/rest/productCate/selectProductCateById.do?ran='+Math.random()+ '&token=' + token,
        	type:'get',
        	data:{cateId:cateId},
        	success:function(data){
        		if(data.retCode=='1003'){
					localStorage.clear();
					window.location.href="login.do";
					return;
				}
        		if(data.retCode==='0000'){
        			if(data.data.parentName==null){
        			      $("#parentName").text("无");
        			}
        			else{
        				 $("#parentName").text(data.data.parentName);
        			}
        			$("#cateId").val(data.data.cateId);
        			$("#cateName").val(data.data.cateName);        			
        			$("#sortNum").val(data.data.sortNum);
        			if(data.data.isValidate==1){
        			   $("#isValidate").attr("checked", true);
        			}
        			else
        			{
        				 $("#isValidate").attr("checked", false);
        			}
        			if(data.data.isBottom==1){
         			   $("#isBottom").attr("checked", true);
         			}
         			else
         			{
         				 $("#isBottom").attr("checked", false);
         			}        		
        		}
        	}
        });
	}	
	
	//表单验证
	var validate = $("#fromEditProductCate").validate({		   
			rules:{					
				cateName:{
					  required:true,
				      remote: { 
		                   url: server_url+'/rest/productCate/checkCateNameEdit.do?timestamp='+Math.random()+ '&token=' + token,
		                   type: "post",
		                   dataType: "json", 
		                   data: {	
		                	         cateId: function() { return $("#cateId").val();},
		                	         cateName: function() { return $("#cateName").val();}
		                         } 
		               },
		               stringMaxLength:30
				},
			    sortNum:{
					   required:true,
					   stringMaxLength:30
				  }	   
			},
			messages:{				
				cateName:{
				      required:"该值不能为空！",
	                  remote:"商品分类名已存在，请重新输入!",
	                  stringMaxLength:"最多能输入30个字符！"
	                 },
	                 sortNum:{
	                	 required:"该值不能为空！",
	                	 stringMaxLength:"最多能输入30个字符！"
	                 }
			},
			submitHandler:function(){
				sub();
			},
			errorPlacement: function(error, element) {
				if ( element.is(":radio") )
					error.appendTo ( element.parent() );
				else if ( element.is(":checkbox") )
					error.appendTo ( element.parent() );
				else if ( element.is("input[name=captcha]") )
					error.appendTo ( element.parent() );
				else
					error.insertAfter(element);
			},
			validClass : 'rightValidate',
		    success: function(label) {
			   label.html("&nbsp;").text(" ") ;
		    }
	 });	

});


//提交
function sub(){	
	if($("#fromEditProductCate").valid()){
		$.ajax({
			url:server_url+'/rest/productCate/productCateEdit.do?ran='+Math.random()+ '&token=' + token,
			type:'post',
			data:$("#fromEditProductCate").serialize(),
			success:function(data){
				easyDialog.open({container:{header:'温馨提示',content:data.retMsg},autoClose: 2000});
				back();
			}
		});
	}
}


function back(){	
	var url="productCateList.do?ran="+Math.random();
	tl_config.loadToWorkArea(url);
}

function submitCommit(){
	$("#fromEditProductCate").submit();
}
